"""
Case Type   : 事务控制
Case Name   : 输入货币类型的变量，匿名块正常执行,匿名块中事务是否同步到主机
Create At   : 2024/7/1
Owner       : peilinqian
Description :
    1.新建测试表
    2.在匿名块中写入货币类型的变量，对数据库表执行插入操作
    3.查看主机事务数据
    4.查看备机事务数据
Expect      :
    1.创建测试表成功
    2.在匿名块中写入货币类型的变量，对数据库表执行插入操作成功
    3.主机事务数据正确
    4.备机事务数据同步成功
History     :
    Modified by peilinqian at 2024-7-2:优化用例，增加主备同步等待
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

pri_sh = CommonSH('PrimaryDbUser')
node_num = pri_sh.get_node_num()


@unittest.skipIf(node_num < 3, '非1主2备环境不执行')
class TransactionFile(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info('----this is setup----')
        self.log.info(f'----{os.path.basename(__file__)}:start----')
        self.sta1_sh = CommonSH('Standby1DbUser')
        self.Constant = Constant()
        self.tb_name = 't_dml_transaction_case0025'

    def test_transaction_file(self):
        step_txt = '----step1: 新建测试表 expect: 成功----'
        self.log.info(step_txt)
        sql = f'drop table if exists {self.tb_name};' \
              f'create table {self.tb_name}(sk money);'
        result = pri_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn(self.Constant.TABLE_CREATE_SUCCESS, result,
                      '执行失败' + step_txt)

        step_txt = '----step2: 在匿名块中写入货币类型的变量，对数据库表执行插入操作 expect: 成功----'
        self.log.info(step_txt)
        sql = f'''declare
        a money;
        begin
        a := 92233720368547758.07;
        insert into {self.tb_name} values (a);
        end;'''
        result = pri_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn(self.Constant.CREATE_ANONYMOUS_BLOCK_SUCCESS_MSG, result,
                      '执行失败' + step_txt)

        step_txt = '----step3: 查看主机数据 expect: 正确----'
        self.log.info(step_txt)
        sql = f'select * from {self.tb_name};'
        result = pri_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn('$92,233,720,368,547,758.07', result,
                      '执行失败' + step_txt)

        self.log.info('----等待主备同步----')
        self.sta1_sh.check_location_consistency('standby', node_num)

        step_txt = '----step4: 查看备机数据 expect: 正确----'
        self.log.info(step_txt)
        result = self.sta1_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn('$92,233,720,368,547,758.07', result,
                      '执行失败' + step_txt)

    def tearDown(self):
        self.log.info('----this is teardown----')
        self.log.info('----清理环境----')
        step_txt = '----删除表 expect:成功----'
        self.log.info(step_txt)
        sql = f'drop table if exists {self.tb_name};'
        result = pri_sh.execut_db_sql(sql)
        self.log.info(result)
        self.log.info(f'----{os.path.basename(__file__)}:end----')
        self.assertIn(self.Constant.TABLE_DROP_SUCCESS, result,
                      '执行失败' + step_txt)
